using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace YouTuber
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        private void DownloadBtn_Click(object sender, RoutedEventArgs e)
        {
            if (cmo.SelectedItem != null)
            {
                SaveFileDialog Sdialog = new SaveFileDialog();
                var cur_video = (cmo.SelectedItem as YoutubeVideo);
                Sdialog.FileName = cur_video.VideoTitle + "." + cur_video.Extention;
              
                if (Sdialog.ShowDialog() == true)
                {
                    startDownload((cmo.SelectedItem as YoutubeVideo).DownloadUrl,  Sdialog.FileName);
                }

            }

        }
        private void startDownload(string link ,string topath)
        {
            WebClient client = new WebClient();
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
            client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
            client.DownloadFileAsync(new Uri(link), topath);
        }
        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            double bytesIn = double.Parse(e.BytesReceived.ToString());
            double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
            double percentage = bytesIn / totalBytes * 100;
            Status.Content = "Downloaded " + e.BytesReceived + " of " + e.TotalBytesToReceive;
            progressbar.Value = int.Parse(Math.Truncate(percentage).ToString());
        }
        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {

            Status.Content = "Completed";

        }

        private async void Urltxtbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            YoutubeUrl DB = new YoutubeUrl();
            progressbar.IsIndeterminate = true;
            try
            {
                cmo.Items.Clear();
                Uri uriResult;
                bool result = Uri.TryCreate(Urltxtbox.Text, UriKind.Absolute, out uriResult);
                if (result)
                {
                    List<YoutubeVideo> ls = await DB.getVideos(Urltxtbox.Text);
                    foreach (YoutubeVideo l in ls)
                    {
                        cmo.Items.Add(l);
                    }
                }
            }
            catch
            {
            }
            progressbar.IsIndeterminate = false;
            
        }
    }
}
